package com.wnxy.pawnbackgroundsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.pawnbackgroundsystem.dao.GoodsImgMapper;
import com.wnxy.pawnbackgroundsystem.entity.GoodsImg;
import com.wnxy.pawnbackgroundsystem.service.IGoodsImgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class GoodsImgServiceImpl extends ServiceImpl<GoodsImgMapper, GoodsImg> implements IGoodsImgService {

    @Resource
    private GoodsImgMapper goodsImgMapper;

    public void addGoodsImg(Integer goodsId,String goodsName, ArrayList list){
        log.info("开始执行添加图片 参数={},{},{}",goodsId,goodsName,list);
        List<GoodsImg> goodsImgs = goodsImgMapper.selectList(new QueryWrapper<GoodsImg>().eq("goods_id", goodsId));
        for (int i = 0,length = list.size();i<length;i++) {
            GoodsImg goodsImg = new GoodsImg();
            String imgName = goodsName+"_1";
            if (goodsImgs.size() != 0){
                String gimgName = goodsImgs.get(goodsImgs.size() - 1).getGimgName();
                String[] s = gimgName.split("_");
                String img1 = s[0];
                Integer img2 = Integer.parseInt(s[1]);
                imgName = img1+img2;
            }
            goodsImg.setGoodsId(goodsId);
            goodsImg.setGimgName(imgName);
            goodsImg.setGimgUrl(list.get(i)+"");
            goodsImgMapper.insert(goodsImg);
        }

    }
    public IPage getGoodsImgByGoodsId(Integer goodsId, Integer pageIndex, Integer pageSize){
        log.info("开始执行查询商品图片 参数={}",goodsId);
        Page page = new Page(pageIndex,pageSize);
        QueryWrapper<GoodsImg> qw = new QueryWrapper<>();
        qw.eq("goods_id",goodsId);
        IPage page1 = goodsImgMapper.selectPage(page, qw);
        return page1;
    }
}
